// pages/add-address/add-address.js
import { URL, SER_KEY } from "../../config.js"
import { POST, GET } from "../../utils/request.js"
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    areaAddress:'',//区域地址
    lat:'',//纬度
    lng:'',//经度
    default:'NO',
    isPhoneNum:false,//验证是不是手机号
    userPhone:'',
    detail:'',//地址信息
    addressInfo:'',
    name:''
  },
  formSubmit: function (e) {
    let that = this;
    let infoArray=e.detail.value;
    console.log('form发生了submit事件，携带数据为：',infoArray)
    let appId = app.globalData.appId;
    let sessionId = wx.getStorageSync('session_id');

    // 判断必填项
    for (const key in infoArray) {
      if (infoArray[key] == '' && key =='name') {
        wx.showModal({
          title: '提示',
          content: '请输入收货人姓名' 
        })
        return
      } else if(infoArray[key] == '' && key =='userPhone'){
        wx.showModal({
          title: '提示',
          content: '请输入收货人联系方式' 
        })
        return
      } else if(infoArray[key] == '' && key =='address'){
        wx.showModal({
          title: '提示',
          content: '请输入区域地址' 
        })
        return
      } else if(infoArray[key] == '' && key =='addressInfo'){
        wx.showModal({
          title: '提示',
          content: '请输入详细地址' 
        })
        return
      }
    }
    let items = that.data.detail
    that.setData({
      name: e.detail.value.name,
      userPhone: e.detail.value.userPhone,
      areaAddress: that.data.areaAddress,
      addressInfo: e.detail.value.addressInfo
    })
    if (!items){
      let data = {
        appId: appId,
        sessionId: sessionId,
        name: e.detail.value.name,
        userPhone: e.detail.value.userPhone,
        address: that.data.areaAddress,
        addressInfo: e.detail.value.addressInfo,
        lat: that.data.lat,
        lng: that.data.lng,
        isdefault: that.data.default
      }
      let url = URL.HTTP_BASE_INTRANET + SER_KEY.SER_KEY12;
      GET(url, data, (resData) => {
        console.log(resData);
        if (resData.data.res == 'SUCCESS') {
          wx.showToast({
            title: '保存成功!',
            icon:'none'
          })
          setTimeout(function () {
            wx.navigateBack({ delta: 1 })
          }, 1000);

        }
      })
    }else{
      let data = {
        appId: appId,
        sessionId: sessionId,
        name: that.data.name,
        userPhone: that.data.userPhone,
        address: that.data.areaAddress,
        addressInfo: that.data.addressInfo,
        lat: that.data.lat,
        lng: that.data.lng,
        isDefault: that.data.default,
        id: items.id
      }
      // console.log(data)
      let url = URL.HTTP_BASE_INTRANET + SER_KEY.SER_KEY16;
      GET(url, data, (resData) => {
        console.log(resData);
        if (resData.data.res =='SUCCESS'){
          wx.showToast({
            title: '修改成功',
          })
          setTimeout(function () {
            wx.navigateBack({ delta: 1 })
          }, 1000);
          
        }
        
      })
    }
   

  },

  // 验证手机号
  isMPStrict(e){
    let that = this;
    let status = /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-7|9])|(?:5[0-3|5-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[1|8|9]))\d{8}$/g.test(e.detail.value);
  
    if(!status){
      wx.showModal({
        title: '提示',
        content: '请输入正确的11位手机号码！' ,
        complete(){
          that.setData({
            userPhone:''
          })
        }
      })
      return
    }

    that.setData({
      isPhoneNum:status
    })

  },

  // 设置默认地址
  setDefault(e){
    // console.log(this.data.default)
    this.setData({
      default:this.data.default=='YES'?'NO':'YES'
    })
    // console.log(this.data.default)
  },
  // 利用微信地图获取地址
  chooseLocation(){
    let that = this;
    wx.chooseLocation({
      success: (res) => {
        console.log(res)
        that.setData({
          areaAddress:res.address,
          lat:res.latitude,
          lng:res.longitude
        })
      },
    })
  },
  // 判断授权获取地址信息
  getAddress:function(){
    let that = this;
    wx.getSetting({
      success: (res) => {
        if (res.authSetting['scope.userLocation']) {
          that.chooseLocation();
        } else {
          wx.authorize({
            scope: 'scope.userLocation',
            success(){
              that.chooseLocation();
            }
          })
        }
      }
    })
   
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options&&options.item){
      let item = JSON.parse(options.item)
      wx.setNavigationBarTitle({
        title: '修改地址' 
      })
    options.item ? 
    this.setData
    ({ 
        detail: item,
        areaAddress: item.address,//区域地址
        lat: item.lat,//纬度
        lng: item.lng,//经度
        default: item.isDefault,
      }):this.getAddress()
    }
    
  },
 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})